package com.logicwin.aibala.model.androidSWInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.pm.*;
import android.util.Log;

import com.logicwin.aibala.com.AibalaException;
import com.logicwin.aibala.com.GlobalConfig;

/**
 * Android软件信息服务类
 * 
 * @author GuoDing
 * @since 2011-05-27
 */
public final class AndroidSWInfoService {
	private AndroidSWInfoService() {
	}
	
	public static final int CLASS_ID_TOP = 0;             //首页
	public static final int CLASS_ID_GAME = 1;            //炫动游戏
	public static final int CLASS_ID_VIDEO = 2;           //影音娱乐
	public static final int CLASS_ID_NETWORKING = 3;      //网络社交
	public static final int CLASS_ID_LIFE = 4;            //生活媒体
	public static final int CLASS_ID_OFFICE = 5;          //商务办公
	public static final int CLASS_ID_UTIL = 6;            //系统工具
	
	public static final int SORT_ID_HOT = 1;
	public static final int SORT_ID_TIME = 2;
	public static final int SORT_ID_RECOMMEND = 3;
	
	private static Map<Integer, SortList> sortMap = null;
	
	private static List<APKListItem> apkList = null;
	
	private static List<APKInstallInfo> apkInstallList = null;
	
	/**
	 * 初始化
	 * @throws AibalaException
	 */
	public static void init(Context ctx) throws AibalaException {
		try {
			/**
			 * 读取当前安装程序信息
			 */
//			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//			DocumentBuilder builder = factory.newDocumentBuilder();
//			Document doc = builder.parse(new File(GlobalConfig.getAPKInstallXML()));			
//			
//			NodeList installList = doc.getElementsByTagName("installInfo");
//			apkInstallList = new ArrayList<APKInstallInfo>();
//			
//			for(int i = 0; i < installList.getLength(); i++) {
//				apkInstallList.add(new APKInstallInfo(installList.item(i)));
//			}
//			
//			PackageManager pm = ctx.getPackageManager();
//			List<PackageInfo> piList = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
//			for(int i = 0; i < piList.size(); i++) {
//				apkInstallList.add(new APKInstallInfo(piList.get(i)));
//			}
			
			/**
			 * 读取网站软件列表信息
			 */
//			factory = DocumentBuilderFactory.newInstance();
//			builder = factory.newDocumentBuilder();
//			doc = builder.parse(new File(GlobalConfig.getAPKListXML()));
//			
//			NodeList softwareList = doc.getElementsByTagName("software");
//			apkList = new ArrayList<APKListItem>();
//			
//			for(int i = 0; i < softwareList.getLength(); i++) {
//				apkList.add(new APKListItem(softwareList.item(i)));
//			}
			
			apkList = new ArrayList<APKListItem>();
			for(int i = 1; i < 60; i++) {
				apkList.add(APKListItem.getInstance(i));
			}
			
//			/**
//			 * 生成排序列表
//			 */
//			sortMap = new HashMap<Integer, SortList>();
//			sortMap.put(CLASS_ID_GAME, new SortList(CLASS_ID_GAME));
//			sortMap.put(CLASS_ID_VIDEO, new SortList(CLASS_ID_VIDEO));
//			sortMap.put(CLASS_ID_NETWORKING, new SortList(CLASS_ID_NETWORKING));
//			sortMap.put(CLASS_ID_LIFE, new SortList(CLASS_ID_LIFE));
//			sortMap.put(CLASS_ID_OFFICE, new SortList(CLASS_ID_OFFICE));
//			sortMap.put(CLASS_ID_UTIL, new SortList(CLASS_ID_UTIL));
//			
//			for(APKListItem ai : apkList) {
//				SortList sl = sortMap.get(ai.getClassID());
//				if(null != sl) {
//					sl.addHotList(ai);
//					sl.addTimeList(ai);
//					sl.addRecommandList(ai);
//				} 
//				else {
//					Log.e("", "swID=" + ai.getID() + " calssID=" + ai.getClassID());
//				}
//			}
			
		} catch(Exception e) {
			Log.e("", "", e);
			throw new AibalaException(e);
		}
	}
	
	/**
	 * 获取软件当前安装状态
	 * @param item
	 */
	public static void checkInstallState(APKListItem item) {
		for(APKInstallInfo ai : apkInstallList) {
			if(ai.getSWID() == item.getID()) {
				item.setState(ai.getCurrState());
				if((ai.getCurrState() == APKFileMgmt.APK_STATE_INSTALL) || (ai.getCurrState() == APKFileMgmt.APK_STATE_DOWN)) {
					if(0 != ai.getCurrVersion().compareTo(item.getVersion())) {
						item.setState(APKFileMgmt.APK_STATE_UPDATE);
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 获取最热列表 
	 * @param classID: 炫动游戏，影音娱乐，网络社交，生活媒体，商务办公，系统工具
	 * @return
	 */
	public static List<APKListItem> getHotList(int classID) {
//		SortList sl = (SortList)sortMap.get(classID);
//		if(null != sl) {
//			return sl.getHotList();
//		}
//		Log.e("", "classID=" + classID);
		
		List<APKListItem> ret = new ArrayList<APKListItem>();
		
		switch(classID) {
			case CLASS_ID_TOP:
//				首页:     (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
				ret.add(apkList.get(0));
				ret.add(apkList.get(1));
				ret.add(apkList.get(2));
				ret.add(apkList.get(3));
				ret.add(apkList.get(4));
				ret.add(apkList.get(5));
				ret.add(apkList.get(6));
				ret.add(apkList.get(7));
				ret.add(apkList.get(8));
				ret.add(apkList.get(9));
				break;
			case CLASS_ID_GAME:
//				炫动游戏：(3, 5, 7, 8, 1, 2, 4, 11,13,14)
				ret.add(apkList.get(2));
				ret.add(apkList.get(4));
				ret.add(apkList.get(6));
				ret.add(apkList.get(7));
				ret.add(apkList.get(0));
				ret.add(apkList.get(1));
				ret.add(apkList.get(3));
				ret.add(apkList.get(10));
				ret.add(apkList.get(12));
				ret.add(apkList.get(13));
				break;
			case CLASS_ID_VIDEO:
//				影音娱乐：(15,16,17,18,19,9, 7, 3, 2, 30)
				ret.add(apkList.get(14));
				ret.add(apkList.get(15));
				ret.add(apkList.get(16));
				ret.add(apkList.get(17));
				ret.add(apkList.get(18));
				ret.add(apkList.get(8));
				ret.add(apkList.get(6));
				ret.add(apkList.get(2));
				ret.add(apkList.get(1));
				ret.add(apkList.get(29));
				break;
			case CLASS_ID_NETWORKING:
//				网络社交：(20,21,22,23,24,12,6, 9, 15,25)
				ret.add(apkList.get(19));
				ret.add(apkList.get(20));
				ret.add(apkList.get(21));
				ret.add(apkList.get(22));
				ret.add(apkList.get(23));
				ret.add(apkList.get(11));
				ret.add(apkList.get(5));
				ret.add(apkList.get(8));
				ret.add(apkList.get(14));
				ret.add(apkList.get(24));
				break;
			case CLASS_ID_LIFE:
//				生活媒体：(25,26,27,28,29,9, 21,19,16,5)
				ret.add(apkList.get(24));
				ret.add(apkList.get(25));
				ret.add(apkList.get(26));
				ret.add(apkList.get(27));
				ret.add(apkList.get(28));
				ret.add(apkList.get(8));
				ret.add(apkList.get(20));
				ret.add(apkList.get(18));
				ret.add(apkList.get(15));
				ret.add(apkList.get(4));
				break;
			case CLASS_ID_OFFICE:
//				商务办公：(30,22,24,16,9, 30,29,26,17,15)
				ret.add(apkList.get(29));
				ret.add(apkList.get(21));
				ret.add(apkList.get(23));
				ret.add(apkList.get(15));
				ret.add(apkList.get(8));
				ret.add(apkList.get(29));
				ret.add(apkList.get(28));
				ret.add(apkList.get(25));
				ret.add(apkList.get(16));
				ret.add(apkList.get(14));
				break;
			case CLASS_ID_UTIL:
//				系统工具：(26,27,21,22,17,18,16,12,23,6)
				ret.add(apkList.get(25));
				ret.add(apkList.get(26));
				ret.add(apkList.get(20));
				ret.add(apkList.get(21));
				ret.add(apkList.get(16));
				ret.add(apkList.get(17));
				ret.add(apkList.get(15));
				ret.add(apkList.get(11));
				ret.add(apkList.get(22));
				ret.add(apkList.get(5));
				break;
			default:
				Log.e("", "classID=" + classID);
				break;
		}
		
		return ret;
	}
	
	/**
	 * 获取最新列表
	 * @param classID: 炫动游戏，影音娱乐，网络社交，生活媒体，商务办公，系统工具
	 * @return
	 */
	public static List<APKListItem> getTimeList(int classID) {
//		SortList sl = (SortList)sortMap.get(classID);
//		if(null != sl) {
//			return sl.getTimeList();
//		}
//		Log.e("", "classID=" + classID);
		return null;
	}
	
	/**
	 * 获取排行列表
	 * @param classID: 炫动游戏，影音娱乐，网络社交，生活媒体，商务办公，系统工具
	 * @return
	 */
	public static List<APKListItem> getRecommandList(int classID) {
//		SortList sl = (SortList)sortMap.get(classID);
//		if(null != sl) {
//			return sl.getRecommandList();
//		}
//		Log.e("", "classID=" + classID);
		return null;
	}
	
	/**
	 * 获取最热游戏列表的下一页
	 * @param classID
	 */
	public static List<APKListItem> loadHotListNextPage(int classID) {
		return null;
	}
	
	/**
	 * 获取最新游戏列表的下一页
	 * @param classID
	 */
	public static List<APKListItem> loadTimeListNextPage(int classID) {
		return null;
	}
	
	/**
	 * 获取排行游戏列表的下一页
	 * @param classID
	 */
	public static List<APKListItem> loadRecommandListNextPage(int classID) {
		return null;
	}
	
	
}

/**
 * 某一个大类下面所有的排序列表组合 (炫动游戏，影音娱乐，网络社交，生活媒体，商务办公，系统工具)
 * 
 * @author GuoDing
 * @since 2011-07-29
 */
class SortList {
	/**
	 * 大类的ID
	 */
	private int myClassID;
	
	/**
	 * 按照下载次数排序
	 */
	private List<APKListItem> hotList;
	
	/**
	 * 按照时间戳排序
	 */
	private List<APKListItem> timeList;
	
	/**
	 * 按照推荐星数量排序
	 */
	private List<APKListItem> recommandList;
	
	public SortList(int classID) {
		this.myClassID = classID;
		
		this.hotList = new LinkedList<APKListItem>();
		this.timeList = new LinkedList<APKListItem>();
		this.recommandList = new LinkedList<APKListItem>();
	}
	
	public int getID() {
		return this.myClassID;
	}
	
	public List<APKListItem> getHotList() {
		return this.hotList;
	}
	
	public List<APKListItem> getTimeList() {
		return this.timeList;
	}
	
	public List<APKListItem> getRecommandList() {
		return this.recommandList;
	}
	
	public void addHotList(APKListItem item) {
		this.hotList.add(item);
//		APKListItem tmp = null;
//		int location = 0;
//		for(; location < this.hotList.size(); location++) {
//			tmp = this.hotList.get(location);
//			if(Integer.valueOf(item.getDownloadCount()) < Integer.valueOf(tmp.getDownloadCount())) {
//				break;
//			}
//		}
//		this.hotList.add(location, item);
	}
	
	public void addTimeList(APKListItem item) {
		this.timeList.add(item);
//		APKListItem tmp = null;
//		int location = 0;
//		for(; location < this.timeList.size(); location++) {
//			tmp = this.timeList.get(location);
//			if(item.getTime().after(tmp.getTime())) {
//				break;
//			}
//		}
//		this.timeList.add(location, item);
	}
	
	public void addRecommandList(APKListItem item) {
		this.recommandList.add(item);
//		APKListItem tmp = null;
//		int location = 0;
//		for(; location < this.recommandList.size(); location++) {
//			tmp = this.recommandList.get(location);
//			if(item.getRefStarCount() < tmp.getRefStarCount()) {
//				break;
//			}
//		}
//		this.recommandList.add(location, item);
	}
}












